<?php 
/***
	Jdb.1.0.php
	name:xiaojia
	email:iatt@qq.com
	qq:273142650
	time:2011.9.3 20:23
***/
	@require_once('jPublic-1.1.php');

	class Jdb {
		
		//编码
		static $encode;
		
		//数据库
		static $con;
		
		//连接数据库
		static function con($database, $server = 'localhost:3306', $username = 'root', $password = '', $encode = 'utf8')
		{
			//设置编码
			self::$encode = $encode;

			$con = mysql_connect($server, $username, $password);
			
			mysql_select_db($database, $con);
			
			self::$con = $con;
			
			return $con;
			
		}
		
		//执行SQL
		static function getRs($sql, $type = '')
		{
			mysql_query('set names ' . self::$encode);
			
			return $type == '' ? mysql_query($sql, self::$con) : mysql_fetch_array(mysql_query($sql, self::$con));
			
		}
		
		//执行SQL，不返回值
		static function execute($sql)
		{
			mysql_unbuffered_query("set names " . self::$encode);
			
			return mysql_unbuffered_query($sql, self::$con);
			
		}
		
		//关闭数据集
		static function clearRS($rs)
		{
			mysql_free_result($rs);
			
		}
		
		//关闭数据库
		static function clearDB()
		{
			mysql_close(self::$con);
			
		}
		
		//数据集条数
		static function rsRows($rs)
		{
			return @mysql_num_rows($rs);
			
		}
		
		//数据集指针
		static function rsSeek($rs, $n = 0 )
		{
			return @mysql_data_seek($rs, $n);
			
		}
		
		//获取所有表
		static function table_list ()
		{
			$table = self::getRs('SHOW TABLES');
			while ( $rs = mysql_fetch_array($table) ) {
				$table_list[] = $rs['Tables_in_' . DATABASE_TABLE];
			}
			return $table_list;
		}
		
		function affected ($t = 0)
		{
			if ($t === 0) {
				if (mysql_affected_rows() > 0) {
					return true;
				} else {
					return false;
				}
			} elseif ($t === 1) {
				return mysql_affected_rows();
			}
		}
		
		//获取表信息
		static function table_info ($table)
		{
			$table = self::getRs("SELECT * FROM `$table` LIMIT 0");
			for ($i = 0 ;$i < mysql_num_fields($table); $i++) {
				$rs = mysql_fetch_field($table, $i);
				$length = $length = mysql_field_len($table, $i);
				$rs -> type == 'string' && $length /= 3;
				$table_info[$rs -> name] = array(
					'name' => $rs -> name,
					'table' => $rs -> table,
					'length' => $length,
					'not_null' => $rs -> not_null,
					'primary' => $rs -> primary_key,
					'multiple' => $rs -> multiple_key,
					'unique' => $rs -> unique_key,
					'numeric' => $rs -> numeric,
					'blob' => $rs -> blob,
					'type' => $rs -> type,
					'unsigned' => $rs -> unsigned,
					'zerofill' => $rs -> zerofill,
				);
			}
			return $table_info;
		}
		
		//所有表信息
		static function table_info_all ()
		{
			$table_list = self::table_list();
			for ($i = 0; $i < count($table_list); $i++) {
				$table_info[$table_list[$i]] = self::table_info($table_list[$i]);
			}
			return $table_info;
		}
		
		//POST数组 表名 类型 去掉的字段 字段正则 后缀
		static function data ($data = array(table => '', type => '', remove => '', regexp => '', pre => '', suf => '', limit => NULL))
		{
			$table_info = self::table_info($data['table']);
			
			switch ($data['type']) {
				case 'POST':
					$array = $_POST;
				break;
				case 'GET':
					$array = $_GET;
				break;
				case 'ALL':
					$array = $_REQUEST;
				break;
			}
			
			foreach ($array as $key => $v) {
				if (!in_array($key, $data['remove'])) {
					$field = preg_replace('/'.$data['regexp'].'/', '', $key);
					$length = $table_info[$field]['length'];
					switch ($data['type']) {
						case 'POST':
							$string[$field] = Jpublic::post($data['pre'].$field.$data['suf'], 0, 1, 0, $length);
						break;
						case 'GET':
							$string[$field] = Jpublic::get($data['pre'].$field.$data['suf'], 0, 1, 0, $length);
						break;
						case 'ALL':
							$string[$field] = Jpublic::request($data['pre'].$field.$data['suf'], 0, 1, 0, $length);
						break;
					}
					if (is_array($data['limit'])) {
						if (is_array($data['limit']['field'])) {
							if ($data['limit']['non'] == true) {
								$non = !in_array($field, $data['limit']['field']);
							} else {
								$non = in_array($field, $data['limit']['field']);
							}
							if ($non) {
								if (preg_match('/'.$data['limit']['regexp'].'/', $string[$field]) == 0) {
									$string = array('error', $field);
									break;
								}
							}
							
						} else {
							if ($data['limit']['field'] == 'all') {
								if (preg_match('/'.$data['limit']['regexp'].'/', $string[$field]) == 0) {
									$string = array('error', $field);
									break;
								}
							} else {
								if (!empty($data['limit']['regexp'][$field])) {
									if (preg_match('/'.$data['limit']['regexp'][$field].'/', $string[$field]) == 0) {
										$string = array('error', $field);
										break;
									}
								}
							}
						}
					}
				}
			}
			
			return $string;
		}
		
		//insert
		static function insert($table, $data, $type = 1)
		{
			foreach($data as $key => $v) { 
				$filed[] = "`$key`";
				$value[] = is_int($v) ? $v : "'$v'";
			}
			
			$filed = '(' . implode(',', $filed) . ')';
			$value = '(' . implode(',', $value) . ')';
			
			$string = "INSERT INTO `$table` $filed VALUES $value";
			
			if ($type == 0) {
				return $string;
			} else {
				self::execute($string);
				return mysql_insert_id();
			}
		}
		
		//update
		static function update($table, $data, $where, $type = 1)
		{
			foreach($data as $key => $v) {
				if (is_int($v)) {
					$filed[] = "`$key` = $v";
				} else {
					$filed[] = "`$key` = '$v'";
				}
			}
			
			$string = "UPDATE `$table` SET " . implode(',', $filed) . ' ' . $where;
			
			if ($type == 0) {
				return $string;
			} else {
				self::execute($string);
			}
		}
		
		//DELETE
		static function delete($table, $where, $type = 1) {
			$string = "DELETE FROM `$table` $where";
			if ($type == 0) {
				return $string;
			} else {
				self::execute($string);
			}
		}
		
		//select
		static function select($rs, $type = 1) {
			return self::getRs($rs, $type);
		}
	}
?>